package com.lagou.task.servlet;

import com.lagou.task.entity.Student;
import com.lagou.task.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StudentDeleteServlet",urlPatterns = "/delete")
public class StudentDeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StudentService studentService = new StudentService();
        List<Student> students = studentService.showAllService();
        String deleteID = request.getParameter("deleteID");
        String[] ids = deleteID.split(" ");
        int deleteNum = ids.length;
        int delete_i = 0;
        // 遍历学生信息和获得到的要删除的id
        for (Student student:students){
            for (String s:ids){
                if (!ids.equals("") && student.getId().equals(s)){
                    delete_i += studentService.deleteStudentService(student);
                }
            }
        }
        if (delete_i == deleteNum){
            response.sendRedirect("show.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
